package com.example.dairenkun.neufoodshop.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.fragment.CollectFragment;
import com.example.dairenkun.neufoodshop.fragment.HomeFragment;
import com.example.dairenkun.neufoodshop.fragment.MyselfFragment;
import com.example.dairenkun.neufoodshop.fragment.SearchFragment;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout mTabHome;
    private LinearLayout mTabCollect;
    private LinearLayout mTabSearch;
    private LinearLayout mTabMyself;

    private ImageButton mImgHome;
    private ImageButton mImgCollect;
    private ImageButton mImgSearch;
    private ImageButton mImgMyself;

    private Fragment mFmHome;
    private Fragment mFmCollect;
    private Fragment mFmSearch;
    private Fragment mFmMyself;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      // this.requestWindowFeature(Window.FEATURE_NO_TITLE);
     setContentView(R.layout.activity_main);
        initView();
        initEvent();
        setSelect(0);
    }

    private void initEvent() {
        mTabHome.setOnClickListener(this);
        mTabCollect.setOnClickListener(this);
        mTabSearch.setOnClickListener(this);
        mTabMyself.setOnClickListener(this);
    }

    private void initView() {
        mTabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        mTabCollect = (LinearLayout) findViewById(R.id.id_tab_collect);
        mTabSearch = (LinearLayout) findViewById(R.id.id_tab_search);
        mTabMyself = (LinearLayout) findViewById(R.id.id_tab_myself);

        mImgHome = (ImageButton) findViewById(R.id.id_tab_home_img);
        mImgCollect = (ImageButton) findViewById(R.id.id_tab_collect_img);
        mImgSearch= (ImageButton) findViewById(R.id.id_tab_search_img);
        mImgMyself = (ImageButton) findViewById(R.id.id_tab_myself_img);

    }


    @Override
    public void onClick(View v) {

        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_home:
                setSelect(0);
                break;
            case R.id.id_tab_collect:
                setSelect(1);
                break;
            case R.id.id_tab_search:
                setSelect(2);
                break;
            case R.id.id_tab_myself:
                setSelect(3);
                break;

            default:
                break;
        }
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmHome == null)
                {
                    mFmHome = new HomeFragment();
                    transaction.add(R.id.id_content, mFmHome);
                } else
                {
                    transaction.show(mFmHome);
                }
                mImgHome.setImageResource(R.drawable.tab_home_pressed);

                break;
            case 1:
                if (mFmCollect == null)
                {
                    mFmCollect = new CollectFragment();transaction.add(R.id.id_content, mFmCollect);
                } else
                {
                    transaction.show(mFmCollect);

                }
                mImgCollect.setImageResource(R.drawable.tab_collect_pressed);
                break;
            case 2:
                if (mFmSearch == null)
                {
                    mFmSearch = new SearchFragment();
                    transaction.add(R.id.id_content, mFmSearch);
                } else
                {
                    transaction.show(mFmSearch);
                }
                mImgSearch.setImageResource(R.drawable.tab_search_pressed);
                break;
            case 3:
                if (mFmMyself == null)
                {
                    mFmMyself = new MyselfFragment();
                    transaction.add(R.id.id_content, mFmMyself);
                } else
                {
                    transaction.show(mFmMyself);
                }
                mImgMyself.setImageResource(R.drawable.tab_myself_pressed);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmHome != null)
        {
            transaction.hide(mFmHome);
        }
        if (mFmCollect != null)
        {
            transaction.hide(mFmCollect);
        }
        if (mFmSearch != null)
        {
            transaction.hide(mFmSearch);
        }
        if (mFmMyself != null)
        {
            transaction.hide(mFmMyself);
        }
    }

    private void resetImgs() {
        mImgHome.setImageResource(R.drawable.tab_home_normal);
        mImgCollect.setImageResource(R.drawable.tab_collect_normal);
        mImgSearch.setImageResource(R.drawable.tab_search_normal);
        mImgMyself.setImageResource(R.drawable.tab_myself_normal);
    }
}
